package main

import (
	"fmt"
)

type binaryTree struct {
	element    int
	leftNode   *binaryTree
	rightNnode *binaryTree
	size       int
}

//返回binaryTree
func getTree() *binaryTree {
	return &binaryTree{}
}

//像以binaryTree为节点的tree，递归操作
func add(element int, root *binaryTree) {
	size := 1
	if root.size == 0 {
		root.element = element
		root.size = 1
		fmt.Printf("5555")
	}
	if element == root.element {
		return
	} else if element < root.element && root.leftNode == nil {
		size++
		root.leftNode = &binaryTree{element: element, size: size}

	} else if element > root.element && root.rightNnode == nil {
		size++
		root.rightNnode = &binaryTree{element: element, size: size}
	}
	size++
	if element < root.element {
		add(element, root.leftNode)
	} else {
		add(element, root.rightNnode)
	}
}

//前序遍历
func preRaversal(root *binaryTree) {
	if root == nil {
		return
	}
	fmt.Printf("value is,%d", root.element)
	if root.leftNode != nil {
		//fmt.Printf("value is,%d",root.leftNode.element)
		preRaversal(root.leftNode)
	}
	if root.rightNnode != nil {
		//fmt.Printf("value is,%d",root.rightNnode.element)
		preRaversal(root.rightNnode)
	}
}

//中序遍历
func midRaversal(root *binaryTree) {
	if root == nil {
		return
	}
	if root.leftNode != nil {
		fmt.Printf("value is,%d", root.leftNode.element)
		preRaversal(root.leftNode.leftNode)
	}
	fmt.Printf("value is,%d", root.element)
	if root.rightNnode != nil {
		fmt.Printf("value is,%d", root.rightNnode.element)
		preRaversal(root.rightNnode.rightNnode)
	}
}

//后序遍历
func main() {
	tree := getTree()
	add(6, tree)
	add(4, tree)
	add(7, tree)
	add(2, tree)
	preRaversal(tree)
	midRaversal(tree)
}
